<?php
/**
 * Created by IntelliJ IDEA.
 * User: breno
 * Date: 21/05/13
 * Time: 09:36
 * To change this template use File | Settings | File Templates.
 */ 
class EstatisticaController extends Controller {

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow authenticated user to perform 'index' action
                'actions' => array('index'),
                'users' => array('@'),
            ),
            array('deny',  // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Shows statistics for the activities according to the filters sent by ajax.
     */
    public function actionIndex() {
        $model = new Estatistica('index');
        $model->unsetAttributes();  // clear any default values

        if(isset($_GET['Estatistica']))
            $model->attributes = $_GET['Estatistica'];

        $model->testAndSetDefaultDates();
        $model->qtd_atividades_por_conclusao = $this->getAmountActivities(
            $model->data_inicial,
            $model->data_final,
            $model->centro_de_operacao,
            $model->orgao
        );

        $this->render('index', array(
            'model' => $model,
            'centros_de_operacao_e_orgaos' => Usuario::model()->findAll(array(
                'select' => 'centro_de_operacao, orgao',
                'condition' => 'orgao <> "" OR centro_de_operacao <> ""')),
        ));
    }

    /**
     * Gets amount of activities according to filters
     */
    private function getAmountActivities($data_inicial, $data_final, $centro_de_operacao, $orgao) {
        if(empty($orgao) && empty($centro_de_operacao))
            $activities = Atividade::model()->findAll(array(
                    'condition' => 'data BETWEEN :from AND :to',
                    'params' => array(
                        ':from' => DateHelper::changeDateFormatToSql($data_inicial),
                        ':to'   => DateHelper::changeDateFormatToSql($data_final)
                    )
                )
            );

        else {
            $conditions = array();
            if(!empty($orgao))
                array_push($conditions, "orgao = '" . $orgao . "'");

            if(!empty($centro_de_operacao))
                array_push($conditions, "centro_de_operacao = '". $centro_de_operacao . "'");

            $condition = implode(' AND ', $conditions);

            $activities = Atividade::model()->with(array(
                    'Usuario' => array('condition' => $condition))
                )->findAll(array(
                        'condition' => 'data BETWEEN :from AND :to',
                        'params' => array(
                            ':from' => DateHelper::changeDateFormatToSql($data_inicial),
                            ':to'   => DateHelper::changeDateFormatToSql($data_final)
                        )
                    )
                );
        }

        $qtdActivities = array(
            ConclusionTypes::NOT_CONCLUDED => 0,
            ConclusionTypes::CONCLUDED_BEFORE_ALERT => 0,
            ConclusionTypes::CONCLUDED_BEFORE_CONCLUSION => 0,
            ConclusionTypes::CONCLUDED_BEFORE_TOLERANCE => 0,
            ConclusionTypes::CONCLUDED_AFTER_TOLERANCE => 0,
        );

        foreach ($activities as $index => $value) {
            switch(intval($value->concluida)) {
                case ConclusionTypes::NOT_CONCLUDED:
                    $qtdActivities[ConclusionTypes::NOT_CONCLUDED]++;
                    break;
                case ConclusionTypes::CONCLUDED_BEFORE_ALERT:
                    $qtdActivities[ConclusionTypes::CONCLUDED_BEFORE_ALERT]++;
                    break;
                case ConclusionTypes::CONCLUDED_BEFORE_CONCLUSION:
                    $qtdActivities[ConclusionTypes::CONCLUDED_BEFORE_CONCLUSION]++;
                    break;
                case ConclusionTypes::CONCLUDED_BEFORE_TOLERANCE:
                    $qtdActivities[ConclusionTypes::CONCLUDED_BEFORE_TOLERANCE]++;
                    break;
                case ConclusionTypes::CONCLUDED_AFTER_TOLERANCE:
                    $qtdActivities[ConclusionTypes::CONCLUDED_AFTER_TOLERANCE]++;
                    break;
            }
        }

        return $qtdActivities;
    }
}